﻿using Infrastructure.Attributes.Ioc;
using Infrastructure.MyException;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Model
{
    public class ConfigurationManagerModel
    {
        public static ConfigurationManager? Configuration { get; set; }
        static readonly Object MyLock = new();
        public static int ThreadSafeReadInt(string key)
        {
            try
            {
                return int.Parse(ThreadSafeReadString(key));
            }
            catch
            {
                throw new ShowUserLogicException($"{key},配置错误");
            }
        }

        public static String ThreadSafeReadString(string key)
        {
            try
            {
                lock (MyLock)
                {
                    var temp = Configuration?[key];

                    if (temp == default)
                        return "";
                    return temp;
                }
            }
            catch
            {
                throw new ShowUserLogicException($"{key},配置错误");
            }
        }

    }
}
